package com.example.myworkgwl01;


import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity
implements View.OnClickListener{

    private Fragment weixinFragment = new weixinFragment();
    private Fragment friendFragment = new friendFragment();
    private Fragment contactFragment = new contactFragment();
    private Fragment settingweixinFragment = new settingweixinFragment();
    private FragmentManager fragmentManager;

    private LinearLayout linearLayout1,linearLayout2,linearLayout3,
            linearLayout4;
    private ImageView IWeixin;
    private ImageView IFriend;
    private ImageView IContact;
    private ImageView ISetting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        linearLayout1=findViewById(R.id.id_01);
        linearLayout2=findViewById(R.id.id_02);
        linearLayout3=findViewById(R.id.id_03);
        linearLayout4=findViewById(R.id.id_04);

        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

        IWeixin =(ImageView)findViewById(R.id.imageView1);
        IFriend =(ImageView)findViewById(R.id.imageView2);
        IContact = (ImageView)findViewById(R.id.imageView3);
        ISetting = (ImageView) findViewById(R.id.imageView4);

        initFragment();
        showfragment(0);

    }


    //可以定义函数更简洁
    private void initFragment(){
         fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.id_content, weixinFragment);
        transaction.add(R.id.id_content, contactFragment);
        transaction.add(R.id.id_content, friendFragment);
        transaction.add(R.id.id_content, settingweixinFragment);
        transaction.commit();
    }
    private void hideFragment(FragmentTransaction transaction){

        transaction.hide(weixinFragment);
        transaction.hide(friendFragment);
        transaction.hide(contactFragment);
        transaction.hide(settingweixinFragment);
    }

    @Override
    public void onClick(View v) {
        becomedark();
        switch (v.getId()){
            case R.id.id_01:
                showfragment(0);
                break;
            case R.id.id_02:
                showfragment(1);
                break;
            case R.id.id_03:
                showfragment(2);
                break;
            case R.id.id_04:
                showfragment(3);
                break;
            default:
                break;
        }
    }
    private void showfragment(int i) {
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(weixinFragment);
                IWeixin.setImageResource(R.drawable.weixin_02);
                break;
            case 1:
                transaction.show(friendFragment);
                IFriend.setImageResource(R.drawable.friend_02);
                break;
            case 2:
                transaction.show(contactFragment);
                IContact.setImageResource(R.drawable.contact_02);
                break;
            case 3:
                transaction.show(settingweixinFragment);
                ISetting.setImageResource(R.drawable.setting_02);
                break;
            default:
                break;
        }
        transaction.commit();
    }
    private void becomedark(){

        IWeixin.setImageResource(R.drawable.weixin_01);
        IFriend.setImageResource(R.drawable.friend_01);
        IContact.setImageResource(R.drawable.contact_01);
        ISetting.setImageResource(R.drawable.setting_01);
    }

}